<?php
class reporterModel extends appModel
{
    public function insertUVPV($type,$sid)
    {
        $now = strtotime(date("Y-m-d",yf_time()));
        $ip = hy_getIP();
        $sql = "SELECT id  FROM ".$this->table("reporter_uvpv")." WHERE ip = '$ip' AND type = $type AND sid = $sid AND time = $now ";
        $id = $this->getOne($sql);
        if($id)
        {//＋1
            $sql = "UPDATE ".$this->table("reporter_uvpv")." SET number = number+1 WHERE id = $id ";
            $this->query($sql);
        }
        else
        {//新增
            $info['ip'] = $ip;
            $info['number'] = 1;
            $info['type'] = $type;
            $info['sid'] = $sid;
            $info['time'] = $now;
            $this->insert($info, "reporter_uvpv");
        }
    }
    
    public function getTab($reporter_id)
    {
        $sql = "SELECT id,tab_title,status FROM ".$this->table("reporter_new")." WHERE reporter_id = $reporter_id AND status > 0 ORDER BY status ASC ";
        return $this->getAll($sql);
    }
    
    public function get_new($reporter_id,$status = 1)
    {
        $sql = "SELECT is_live FROM ".$this->table("reporter")." WHERE id = $reporter_id LIMIT 1";
        $isLive = $this->getOne($sql);
        if($isLive>0){//直播
            $sql = "SELECT p1.id,p1.title,p1.new_desc,p1.video_url,p1.comment_num,p1.like_num,p1.createtime,p1.reporter_id,p1.status,p2.reporter_name,p2.reporter_url,p2.gratuity_url FROM ".$this->table("reporter_new").
            " p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.reporter_id = p2.id WHERE p1.reporter_id = $reporter_id  AND p1.type = 1 LIMIT 1 ";
            $result = $this->getRow($sql);
        }else{//正常
            $sql = "SELECT p1.id,p1.title,p1.new_desc,p1.video_url,p1.comment_num,p1.like_num,p1.createtime,p1.reporter_id,p1.status,p2.reporter_name,p2.reporter_url,p2.gratuity_url FROM ".$this->table("reporter_new").
            " p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.reporter_id = p2.id WHERE p1.reporter_id = $reporter_id AND p1.status = $status AND p1.type = 0 LIMIT 1 ";
            $result = $this->getRow($sql);
            if(!$result){//默认
                $sql = "SELECT p1.id,p1.title,p1.new_desc,p1.video_url,p1.comment_num,p1.like_num,p1.createtime,p1.reporter_id,p1.status,p2.reporter_name,p2.reporter_url,p2.gratuity_url FROM ".$this->table("reporter_new").
                " p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.reporter_id = p2.id WHERE p1.reporter_id = $reporter_id AND p1.status != 0 AND p1.type = 0  ORDER BY status ASC LIMIT 1 ";
                $result = $this->getRow($sql);
            }
        }
        
        if($result){
            $where = " p1.status = 1 AND p1.new_id = ".$result['id']." ORDER BY create_time DESC ";
            $sql="SELECT p1.id,p2.headimgurl,p2.nickname,p1.comment FROM ".$this->table('reporter_new_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
            $comment_list = $this->getAll($sql);
            foreach ($comment_list as $k=>$v)
            {
                if(preg_match("/\/0$/", $v['headimgurl']))
                {
                    $comment_list[$k]['headimgurl']=substr($v['headimgurl'],0,strlen($v['headimgurl'])-1)."64";
                }
            }
            $sql = "SELECT is_liked FROM ".$this->table("reporter_new_liked")." WHERE new_id=".$result['id']." AND wxid = '".$_SESSION['wxid']."' LIMIT 1 ";
            $status = $this->getOne($sql);
            $result['is_liked'] = $status;
            $result['is_live'] = $isLive;
            $result['comment_list'] = $comment_list;
            return $result;
        }else{
            return array();
        }
    }
    
    public function get_new_info($new_id)
    {
        $sql = "SELECT p1.id,p1.title,p1.new_desc,p1.video_url,p1.comment_num,p1.like_num,p1.createtime,p1.reporter_id,p1.status,p2.reporter_name,p2.reporter_url,p2.gratuity_url,p1.type FROM ".$this->table("reporter_new").
        " p1 LEFT JOIN ".$this->table("reporter")." p2 ON p1.reporter_id = p2.id WHERE p1.id = $new_id LIMIT 1 ";
        $result = $this->getRow($sql);
        if($result){
            $where = " p1.status = 1 AND p1.new_id = ".$result['id']." ORDER BY create_time DESC ";
            $sql="SELECT p1.id,p2.headimgurl,p2.nickname,p1.comment FROM ".$this->table('reporter_new_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
            $comment_list = $this->getAll($sql);
            foreach ($comment_list as $k=>$v)
            {
                if(preg_match("/\/0$/", $v['headimgurl']))
                {
                    $comment_list[$k]['headimgurl']=substr($v['headimgurl'],0,strlen($v['headimgurl'])-1)."64";
                }
            }
            $sql = "SELECT is_liked FROM ".$this->table("reporter_new_liked")." WHERE new_id=".$result['id']." AND wxid = '".$_SESSION['wxid']."' LIMIT 1 ";
            $status = $this->getOne($sql);
            $result['is_liked'] = $status;
            $result['comment_list'] = $comment_list;
            return $result;
        }else{
            return array();
        }
    }
    
    public function zan($wxid,$new_id)
    {
        $sql = "SELECT id,is_liked FROM ".$this->table('reporter_new_liked')." WHERE wxid = $wxid AND new_id = $new_id LIMIT 1 ";
        $row = $this->db->getRow($sql);
        if($row)//有值
        {
            if($row['is_liked']==1)//取消
            {
                $info['is_liked'] = 0 ;
                $result['id'] = $this->update($info, array("table"=>"reporter_new_liked","id"=>$row['id']));
                $result['codeMsg'] = "cancel";
                $this->add_sub_number("reporter_new","like_num", "-1", $new_id);
    
            }else//收藏
            {
                $info['is_liked'] = 1 ;
                $result['id'] = $this->update($info, array("table"=>"reporter_new_liked","id"=>$row['id']));
                $result['codeMsg'] = "like";
                $this->add_sub_number("reporter_new","like_num", "+1", $new_id);
            }
        }else//无值
        {
            $info['is_liked'] = 1;
            $info['new_id'] = $new_id;
            $info['wxid'] = $wxid;
            $result['id'] = $this->insert($info, "reporter_new_liked");
            $result['codeMsg'] = "like";
            $this->add_sub_number("reporter_new","like_num", "+1", $new_id);
        }
        $sql = "SELECT like_num FROM ".$this->table("reporter_new")." WHERE id = $new_id LIMIT 1";
        $result['like_num'] = $this->getOne($sql);
        return $result;
    }
    
    public function add_sub_number($table,$field,$number,$id)
    {
        $sql = "UPDATE ".$this->table($table). " SET $field = $field $number WHERE id = $id ";
        $this->query($sql);
    }
    
    public function sendComment($wxid,$comment,$new_id)
    {
        $info['status'] = 0;
        $info['create_time'] = yf_time();
        $info['wxid'] = $wxid;
        $info['new_id'] = $new_id;
        $info['comment'] = $comment;
        $result = $this->insert($info, "reporter_new_comment");
        if($result){
            $this->add_sub_number("reporter_new", "comment_num", "+1", $new_id);
            $user = $this->get_record($wxid);
            $sql = "SELECT comment_num FROM ".$this->table("reporter_new")." WHERE id = $new_id LIMIT 1";
            $comment_num = $this->getOne($sql);
            return array("error"=>0,"content"=>"评论成功","id"=>$result,"user"=>$user,"comment_num"=>$comment_num);
        }else{
            return array("error"=>1,"content"=>"评论保存失败");
        }
    }
    
    /**
     *获得一条微信record记录
     */
    public function get_record($id)
    {
        if(!$id)
        {
            return false;
        }
        $sql = "SELECT wxid,nickname,headimgurl FROM ".$this->table('wxusers')." WHERE wxid =".$id;
        $result=$this->getRow($sql);
    
        if($result)
        {
    
            return $result;
        }
        else
        {
            return false;
        }
    }
    
    public function getHomeInfo()
    {
        $sql = "SELECT url,pic_path FROM ".$this->table("block_content")." WHERE block_id = '24' AND id != '76'  ORDER BY ordering ASC ";
        $result['homeBanner'] = $this->getAll($sql);
        $sql = " SELECT reporter_place,url,pic_path FROM ".$this->table('block_content')." WHERE id = 76 LIMIT 1 ";
        $block = $this->getRow($sql);
        if($block['reporter_place']<1){
            $isPush = true;
        }else{
            $isPush = false;
        }
        $sql = "SELECT id,reporter_name,reporter_url,ordering,thumb_url,title,home_desc FROM ".$this->table("reporter")." WHERE reporter_name != '' AND ordering > 0  ORDER BY ordering ASC ";//显示的
        $showReporters = $this->getAll($sql);
        $i = 1;
        $reporters = array();
        foreach($showReporters as  $k => $v)
        {
            if($block['reporter_place']>0){
                if($i==$block['reporter_place']){
                    $isPush = true;
                    $reporters[] = $block;
                }
            }
            $v['type'] = "reporter";
            $reporters[] = $v;
            $i++;
        }
        if(!$isPush)
        {
            $reporters[] = $block;
        }
        $result['reporters'] = $reporters;
        return $result;
    }
}